package com.serverapk.util;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;

import android.util.Log;

import com.serverapk.BaseApplication;

public class GpioUtil {
    public static String TAG = "GpioUtil";

    public static boolean gpio_output_high(String gpio) {
        boolean ret = false;
        String strExport = "echo " + gpio + " > /sys/class/gpio/export";
        String strGpio = "/sys/class/gpio/gpio" + gpio;
        String strGpioDirection = "echo high > " + strGpio + "/direction";
        execShellCmd(strExport);

        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        File file = new File(strGpio);
        if (file.exists()) {
            execShellCmd(strGpioDirection);
            ret = true;
        }
        return ret;
    }

    public static void execShellCmd(String cmd) {
        try {
            Process process = Runtime.getRuntime().exec("su");
            OutputStream outputStream = process.getOutputStream();
            DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
            Log.d(TAG, "[TONY] cmd=" + cmd);
            dataOutputStream.writeBytes(cmd);
            dataOutputStream.flush();
            dataOutputStream.close();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static boolean gpio_input(String gpio) {
        boolean ret = false;
        String strExport = "echo " + gpio + " > /sys/class/gpio/export";
        String strGpio = "/sys/class/gpio/gpio" + gpio;
        String strGpioDirection = "echo in > " + strGpio + "/direction";
        execShellCmd(strExport);

        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        File file = new File(strGpio);
        if (file.exists()) {
            execShellCmd(strGpioDirection);
            ret = true;
        }
        return ret;
    }

    public static boolean gpio_output(String gpio, int state) {
        boolean ret = false;
        String strGpio = "/sys/class/gpio/gpio" + gpio;
        String strGpioDirection = "echo " + state + " > " + strGpio + "/value";

        File file = new File(strGpio);
        if (file.exists()) {
            execShellCmd(strGpioDirection);
            ret = true;
        }
        return ret;
    }

    public static boolean checkGpioExists(String gpio) {
        String strGpio = "/sys/class/gpio/gpio" + gpio;
        File file = new File(strGpio);
        if (file.exists()) {
            return true;
        }
        return false;
    }

    public static int checkGpioStatus(String gpio) {
        char[] buffer = new char[1];
        String strGpio = "/sys/class/gpio/gpio" + gpio + "/value";
        File file = new File(strGpio);
        if (file.exists()) {
            try {
                FileReader reader = new FileReader(file);
                BufferedReader bReader = new BufferedReader(reader);
                bReader.read(buffer);
                bReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return Integer.parseInt(String.valueOf(buffer[0]));
    }

}
